import './index.scss'
import { useMount } from 'ahooks'
import { useRequest } from 'alova'
import { lazy, Suspense, useState } from 'react'

import { Http } from '../Api'
// import { debuggerFun } from '../Utils/Judgment'

import { Decrypt, Encrypt } from '../Utils/IndexedDB'
import { IndexedDBResetting, AddIndexdDB, PermutationData, EncryptedData } from '../Utils/IndexedDB'

const LoadSvg = lazy(() => import('../View/Loading/LoadSvg'))
const { hostname } = window.location

const LoginPage = () => {
    const [Data, setData] = useState()
    const [Template, setTemplate] = useState()
    // 临时模拟
    const { send: PreInfo } = useRequest(params => Http({ url: 'getProjectConfigInfo', params }), { immediate: false })
    const { send } = useRequest(params => Http({ url: 'getProject', params }), { immediate: false })
    // const { send: posText } = useRequest(params => Http({ url: 'posText', params }), { immediate: false })
    const setConfig = async (e) => {
        const { client_id, ...config } = await PermutationData(e)
        await AddIndexdDB('openPage', '')
        await AddIndexdDB('system', { client_id })

        setData(config)
        setTemplate(lazy(() => import(`./LogBox/${client_id}.jsx`)))

        // todo 数据模拟
        // const data = {
        //     theme: 'A01',
        //     logo: "A01",
        //     menu: "A01",
        //     head: "A01",
        //     body: "A01",
        //     userInfo: {
        //         Avatar: 'AvatarA05',
        //         Backboard: 'BackboardA10',
        //         NickName: '飞越疯人院'
        //     },
        //     themeConfig: {
        //         Color: "#13c2c2",
        //         BackType: 'Color',
        //         BackValue: '#F4F5FA',
        //         BackMask: 0,
        //     },
        // }
        // // console.log(data)
        // const enc = await Encrypt(data)
        // const dec = await Decrypt(enc)
        // console.log('加密数据', enc)
        // console.log('解密数据', dec)



    }
    useMount(async () => {
        // 数据请求
        // const data = { a: 1, b: '2', c: {} }
        // const params = await EncryptedData(data)
        // console.log('创建加密数据', params)

        // posText(params).then(res => {
        //     console.log(res)
        // })

        IndexedDBResetting()
        sessionStorage.clear()
        send({ hostname })
            .then(setConfig)
            .catch(err => console.log('失败->重置', err))

        PreInfo({ WebUrl: 'common-lib.ljkjkf.com' }).then(res => {
            const { config, key } = res
            console.log('加密KEY', key)
            console.log('获取项目基础信息', config)





        })




    })
    return (
        <div className='LoginPage'>
            <Suspense fallback={<LoadSvg />}>
                {Data && Template ? <Template config={Data} /> : <LoadSvg />}
            </Suspense>
        </div>
    );
};

export default LoginPage;